package me.yushuai.wukong.base.controller;

import me.yushuai.wukong.base.api.PackingApi;
import me.yushuai.wukong.base.api.dto.*;
import me.yushuai.wukong.base.api.vo.PackingVO;
import me.yushuai.wukong.base.convertor.PackingConvertor;
import me.yushuai.wukong.base.service.PackingService;
import me.yushuai.wukong.common.dto.PageDTO;
import me.yushuai.wukong.common.error.WuKongWebException;
import me.yushuai.wukong.common.exceptin.DuplicateBizIdException;
import me.yushuai.wukong.common.exceptin.NotExistException;
import me.yushuai.wukong.common.mvc.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 包规Web控制器
 *
 * @author zhoushuai@189.cn
 * @since 2022-08-18
 */
@RestController
public class PackingController extends BaseController implements PackingApi {

    /**
     * 包规业务类
     */
    private final PackingService packingService;

    /**
     * 业务对象转换器
     */
    private final PackingConvertor packingConvertor;

    @Autowired
    public PackingController(PackingService packingService, PackingConvertor packingConvertor) {
        this.packingService = packingService;
        this.packingConvertor = packingConvertor;
    }

    @Override
    public Long createPacking(CreatePackingDTO dto) throws WuKongWebException {
        try {
            return this.packingService.createPacking(dto);
        } catch (DuplicateBizIdException e) {
            final Object[] params = {e.getEntityType()};
            final String bizId = e.getBizId();
            throw new WuKongWebException(super.messageSource.getMessage("Packing.bizId.NotFound", params,
                    "The packing bizId :\"" + bizId + "\" is exists.", super.getCurrentLocale()));
        }

    }

    @Override
    public Long editPacking(Long id, UpdatePackingDTO dto) throws WuKongWebException {
        try {
            dto.setPackingId(id);
            return this.packingService.updatePackingById(dto);
        } catch (NotExistException e) {
            final Object[] params = {e.getId()};
            throw new WuKongWebException(
                    super.messageSource.getMessage("Packing.id.NotFound", params,
                            "the id of Packing \"" + id + "\" is not found.", super.getCurrentLocale())
            );
        }
    }

    @Override
    public Long deletePacking(Long id) throws WuKongWebException {
        try {
            this.packingService.deletePacking(id);
            return id;
        } catch (NotExistException e) {
            final Object[] params = {e.getId()};
            throw new WuKongWebException(
                    super.messageSource.getMessage("Packing.id.NotFound", params,
                            "the id of Packing \"" + id + "\" is not found.", super.getCurrentLocale())
            );
        }
    }

    @Override
    public PackingDTO getById(Long id) throws NotExistException {
        return this.packingService.findPackingById(id);
    }

    @Override
    public PageDTO<PackingVO> pagingSearchPacking(PagingSearchPacking dto) {
        final PageDTO<PackingDTO> packingPage = this.packingService.pagingSearchPacking(dto);
        final List<PackingVO> packingVoList = this.packingConvertor.mapToPackingVoList(packingPage.getData());
        return PageDTO.of(dto, packingVoList);
    }


    @Override
    public Long createPackingUom(Long packingId, PackingUomDTO createPackingUomDTO) throws WuKongWebException {
        try {
            createPackingUomDTO.setPackingId(packingId);
            return this.packingService.createPackingUom(createPackingUomDTO);
        } catch (DuplicateBizIdException e) {
            final Object[] params = {e.getBizId()};
            throw new WuKongWebException(super.messageSource.getMessage("PackingUom.bizId.HasExits", params,
                    "The bizId of PackingUom \"" + e.getBizId() + "\" is has exist.", super.getCurrentLocale()));
        }
    }

    @Override
    public Long editPackingUom(Long packingId, Long packingUomId, PackingUomDTO dto) throws WuKongWebException {
        dto.setPackingId(packingId);
        dto.setUomId(packingUomId);
        try {
            return this.packingService.updatePackingUomById(dto);
        } catch (NotExistException e) {
            Object[] params = {e.getId()};
            throw new WuKongWebException(super.messageSource.getMessage("PackingUom.id.NotFound", params,
                    "The id of PackingUom \"" + e.getId() + "\" is not found.", super.getCurrentLocale()));
        }
    }

    @Override
    public Long deletePackingUom(Long packingId, Long packingUomId) throws WuKongWebException {
        try {
            this.packingService.deletePackingUom(packingId, packingUomId);
        } catch (NotExistException e) {
            final Object[] params = {packingId, packingUomId};
            throw new WuKongWebException(super.messageSource.getMessage("PackingUom.id.NotFound", params,
                    "the id of PackingUom \"" + packingUomId + "\" is not found.", getCurrentLocale()));
        }

        return packingUomId;
    }

    @Override
    public PackingUomDTO getById(Long packingId, Long packingUomId) throws NotExistException {
        return this.packingService.findPackingUomById(packingId, packingUomId);
    }

}
